package com.inforeach.util.string;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SimpleEscapingStringTranformer_utest
{
    IStringTransformer sut_;

    @Before
    public void setUp()
    {
        sut_ = new SimpleEscapingStringTransformer();
    }

    void checkSame(String str)
    {
        Assert.assertSame("forward", str, sut_.forward(str));
        Assert.assertSame("reverse", str, sut_.reverse(str));
    }

    void check(String str, String fwd)
    {
        Assert.assertEquals("forward", fwd, sut_.forward(str));
        Assert.assertEquals("reverse", str, sut_.reverse(fwd));
    }

    @Test
    public void testEmpty()
    {
        checkSame("");
    }

    @Test
    public void testNull()
    {
        checkSame(null);
    }

    @Test
    public void testNormal()
    {
        checkSame("abcdefghijklmnopqrstuvwxyz");
        checkSame("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        checkSame("0123456789");
        checkSame("~!@#$%^&*()_+{}:|<>?");
    }

    @Test
    public void testQuote()
    {
        checkSame("\'\"");
    }

    @Test
    public void testSlash()
    {
        check("abc\\def", "abc\\\\def");
    }

    @Test
    public void testOther()
    {
        check("0\n\r1\f23\b456\t", "0\\n\\r1\\f23\\b456\\t");
    }

    @Test
    public void testReverse_SlashAtEnd()
    {
        Assert.assertSame("reverse", "abc\\", sut_.reverse("abc\\"));
        Assert.assertEquals("reverse", "abc\n\\", sut_.reverse("abc\\n\\"));
    }

    @Test
    public void testReverse_EscUnknown()
    {
        Assert.assertSame("reverse", "abc\\z", sut_.reverse("abc\\z"));
        Assert.assertEquals("reverse", "abc\n\\z", sut_.reverse("abc\\n\\z"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testCreate_Empty()
    {
        new SimpleEscapingStringTransformer("");
    }

    @Test(expected = NullPointerException.class)
    public void testCreate_Null()
    {
        new SimpleEscapingStringTransformer(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testCreate_Even()
    {
        new SimpleEscapingStringTransformer("\nn");
    }

}
